<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :append-to-body="true"
      @close="handleClose"
    >
      <el-form label-width="80px" :model="formDate" :rules="rules" ref="ruleForm">
        <el-form-item label="开发阶段:" prop="trackingStepId">
          <el-select v-model="formDate.trackingStepId" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in developStepList"
              :key="item.trackingStepId"
              size="small"
              :label="item.stepName"
              :value="item.trackingStepId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进目的:" prop="purposeTitle">
          <el-input
            type="text"
            placeholder="请输入跟进目的"
            v-model="formDate.purposeTitle"
            size="small"
            clearable
            style="width: 60%"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="目的说明:" prop="introduce">
            <el-input
              type="textarea"
              :rows="4"
              placeholder="请输入目的说明"
              v-model="formDate.introduce"
              clearable
              style="width: 80%"
              maxlength="200"
              show-word-limit
            >
            </el-input>
          </el-form-item>
        <el-form-item label="排序:" prop="sortOrder">
           <el-input-number
            v-model="formDate.sortOrder"
            :min="0"
            label="请排序">
            </el-input-number>
        </el-form-item>
        <el-form-item label="状态:">
          <el-radio-group v-model="formDate.isEnable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getDevelopStepListAPI, addPurposeEditAPI, getPurposeDetailAPI } from '@/api/fms.js'
const defaultListQuery = {
  trackingStepId:"",//开发阶段ID
  purposeTitle: "", //跟进目的
  sortOrder: 0,//排序值
  isEnable: 0,//是否启用
  introduce: "",//目的说明
};
export default {
  name: "addFollowPurpose",
  data() {
    return {
      dialogVisible: false,
      formDate: JSON.parse(JSON.stringify(defaultListQuery)),
      developStepList: [],
      title:"",
      rules: {
        trackingStepId: [
          { required: true, message: '请选择阶段名称', trigger: 'blur' },
        ],
        purposeTitle: [
          { required: true, message: '请输入跟进目的', trigger: 'blur' },
        ],
        introduce: [
          { required: true, message: '请输入目的说明', trigger: 'blur' },
        ]
      }
    };
  },
  created() {
    getDevelopStepListAPI({
      pageNum: 1,
      pageSize: 100,
    }).then((res) => {
      res.list.forEach((item) => {
        this.developStepList.push({
          stepName: item.stepName,
          trackingStepId:item.stepId
       })
      })
    }).catch(() => {})
  },
  methods: {
    // 提交
    btnOk() {
      let res = ""
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.formDate.userId) {
            res = await addPurposeEditAPI(this.formDate);
          } else {
            res = await addPurposeEditAPI(this.formDate);
          }
          if (res) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.handleClose();
            this.$emit("upload");
          }
        } else {
          return false;
        }
      });
    },
    // 回显
    async showDetail(purposeId) {
      this.dialogVisible = true;
      this.title = purposeId ? "修改" : "新增"
      if (purposeId) {
        const res = await getPurposeDetailAPI({
          purposeId: purposeId
        })
        this.formDate = { ...res }
      }
    },
    // 关闭弹框
    handleClose() {
      this.formDate = JSON.parse(JSON.stringify(defaultListQuery))
      this.$refs.ruleForm.resetFields();
      this.dialogVisible = false;
    },
  },
};
</script>
<style lang="scss">
.avatar-uploader-icon {
  margin-bottom: 10px;
  display: inline-block;
  width: 80px;
  height: 80px;
  border: 1px solid #dcdfe6;
  text-align: center;
  line-height: 80px;
  vertical-align: middle;
}
</style>
